﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

using XCode;
using BingZi.HY.Core;



namespace BingZi.HY.Core
{
	/// <summary>
	/// 帮助信息
	/// </summary>
    public partial class app_Help : HYEntityBase<app_Help>
	{
		#region 对象操作
		//基类Entity中包含三个对象操作：Insert、Update、Delete
		//你可以重载它们，以改变它们的行为
		//如：
		/*
		/// <summary>
		/// 已重载。把该对象插入到数据库。这里可以做数据插入前的检查
		/// </summary>
		/// <returns>影响的行数</returns>
		public override Int32 Insert()
		{
			return base.Insert();
		}
		 * */
		#endregion
		
		#region 扩展属性
		//TODO: 本类与哪些类有关联，可以在这里放置一个属性，使用延迟加载的方式获取关联对象

		/*
		private Category _Category;
		/// <summary>该商品所对应的类别</summary>
		public Category Category
		{
			get
			{
				if (_Category == null && CategoryID > 0 && !Dirtys.ContainKey("Category"))
				{
					_Category = Category.FindByKey(CategoryID);
					Dirtys.Add("Category", true);
				}
				return _Category;
			}
			set { _Category = value; }
		}
		 * */
		#endregion

		#region 扩展查询
 
		#endregion

        #region 高级查询
        /// <summary>
        /// 根据上级编号查询帮助信息
        /// </summary>
        /// <returns></returns>
        public static EntityList<app_Help> FindChildByParentID(Int32 ParentID)
        {
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Orderfld = "H_Level,H_ShowOrder";
            qp.OrderType = 0;
            int RecordCount = 0;
            qp.Where.Add(new NewLife.SearchParam(_.H_ParentID, ParentID, NewLife.SearchType.Equal));
            qp.Where.Add(new NewLife.SearchParam(_.H_Delete,0, NewLife.SearchType.Equal));
            return FindAll(qp, out RecordCount);
        }


        #endregion

        #region 扩展操作
        #endregion

        #region 业务


        /// <summary>
        /// 获取部门标题路径
        /// </summary>
        /// <param name="GroupID">部门ID</param>
        /// <returns></returns>
        public static string GetHelpTitle(int HelpID)
        {
            int TotalRecord = 0;
            NewLife.QueryParam qp = new NewLife.QueryParam();
            //qp.Where = " where GroupID =  " + GroupID;
            qp.Where.Add(new NewLife.SearchParam(_.HelpID, HelpID, NewLife.SearchType.Equal));
            qp.PageIndex = 1;
            qp.PageSize = 1;
            List<app_Help> lst = FindAll(qp, out TotalRecord);
            if (TotalRecord == 1)
            {
                foreach (app_Help x in lst)
                {
                    return GetHelpTitle(x.H_ParentID) + ">" + string.Format("<a href='HelpList.aspx?HelpID={0}'>{1}</a>", x.HelpID, x.H_Name);
                }
            }
            return "";
        }

        /// <summary>
        /// 获取当前分类及子分类列表以,号分开
        /// </summary>
        /// <param name="GroupID">部门ID</param>
        /// <returns></returns>
        public static string GetHelpCatID(int HelpID)
        {
            string All_ID = HelpID.ToString();
            int TotalRecord = 0;
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Where.Add(new NewLife.SearchParam(app_Help._.H_ParentID, HelpID, NewLife.SearchType.Equal));
            //qp.Where = " where G_ParentID =  " + GroupID;
            List<app_Help> lst = FindAll(qp, out TotalRecord);
            if (TotalRecord > 0)
            {

                foreach (app_Help x in lst)
                {
                    All_ID = string.Format("{0},{1}", All_ID, GetHelpCatID(x.HelpID));
                    //All_ID + "," + GetGroupCatID(x.GroupID);

                }
            }
            return All_ID;
        }

        /// <summary>
        /// 排序分类ID子分类
        /// </summary>
        /// <param name="GroupID">分类ID</param>
        public static void app_Help_By(int GroupID)
        {
            int RecordCount = 0;
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Orderfld = "H_Level,H_ShowOrder";
            qp.OrderType = 0;
            qp.Where.Add(new NewLife.SearchParam(app_Help._.H_ParentID, GroupID, NewLife.SearchType.Equal));
            qp.Where.Add(new NewLife.SearchParam(app_Help._.H_Delete, 0, NewLife.SearchType.Equal));
            //qp.Where = string.Format("Where G_ParentID ={0} and G_Delete=0 ", GroupID);
            List<app_Help> lst = FindAll(qp, out RecordCount);
            RecordCount = 1;
            foreach (app_Help var in lst)
            {
                var.H_ShowOrder = RecordCount;
                var.Update();
                //Update_Table_Fileds("sys_Group", string.Format("G_ShowOrder={0}", RecordCount), string.Format("GroupID={0}", var.GroupID));
                RecordCount++;
            }
        }
        #endregion
    }
}